<!DOCTYPE html>
<html lang='en'>

<head>
    <meta charset='UTF-8'>
    <meta name='viewport' content='width=device-width, initial-scale=1.0'>
    <meta http-equiv='X-UA-Compatible' content='ie=edge'>
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            border: none;
            outline: none;
            box-sizing: border-box;
        }

        li {
            list-style: none;
        }

        .container {
            margin-top: 1em;
            width: 1500px;
            height: 1000px;
            overflow: auto;

            
            display: grid;
            grid-template-columns: 1fr 1fr;
            grid-template-rows: max-content;
            gap: 0.5em;
        }

        .container .box {
            padding: 1em;

            /* box-shadow: 1px 1px 2px 1px #333; */
        }

        .box .item {
            margin-top: 1em;
            border-top: 1px solid #333;
            padding-top: 1em;

            display: grid;
            grid-template-columns: repeat(auto-fit, 8em);
            /* place-content:space-between; */
            gap: 0.5em;
        }

        .item li {
            padding: 0.5em;
            border: 1px solid #666;
            box-shadow: 0 0 2px gray;
            text-align: center;
        }

        .item li:hover {
            cursor: pointer;
            color: white;
            background-color: #007d20;
        }

        #selected li {
            color: white;
            background-color: #007d20;
        }

        #selected li:hover {
            color: #000;
            background-color: skyblue;
        }
    </style>
</head>

<body>
    <div class="container">
        <div class="box">
            <h2>你的爱好:</h2>
            <ul id="selected" class="item"></ul>
        </div>
        <div class="box">
            <h2>从下面选择爱好:</h2>
            <ul id="unselected" class="item">
                <li>摄影</li>
                <li>编程</li>
                <li>游戏</li>
                <li>旅游</li>
                <li>驾驶</li>
                <li>文学</li>
                <li>艺术</li>
                <li>电影</li>
                <li>交友</li>
                <li>潜水</li>
            </ul>
        </div>
    </div>
</body>

</html>
<script>
    /*

    */
    // 移动关键是利用appendchild一个特性：不同父元素添加同一个元素时，以前的位置会删除，最终出现在最后的位置
    const left = document.querySelector('#selected');
    const right = document.querySelector('#unselected');

    right.addEventListener('click', ulAdd, false);
    function ulAdd(e) {
        var e = e || window.event
        //   判断是否有子元素，防止一次多选择,childElementCount只读属性返回指定父元素的子元素的数量。        
        if (e.target.childElementCount == 0) {
            // e.target.style.background = 'pink'
            // console.log(e.target);
            left.appendChild(e.target)
        };
    }

    left.addEventListener('click', ulDel, false);
    function ulDel(e) {
        var e = e || window.event
        if (e.target.childElementCount == 0) {
            // e.target.style.background = 'white'
            right.appendChild(e.target)
        };
    }

</script>